package user_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/utils/jwts"
	"muziai-server/utils/pwd"
	"muziai-server/utils/res"
)

type BindPasswordRequest struct {
	Pwd string `json:"Pwd" binding:"required"`
}

func (u UserApi) BindPasswordView(c *gin.Context) {
	var cr BindPasswordRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)
	var user models.UserModel
	global.DB.Take(&user, claims.UserID)
	if user.Password != "" {
		res.FailWithMsg(c, "已有密码,不能进行绑定操作")
		return
	}
	passwords, _ := pwd.HashPassword(cr.Pwd)
	global.DB.Model(&user).Update("password", passwords)
	res.OkWithMsg(c, "密码绑定成功")
}
